﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using ShopOnline.BLL;

namespace ShopOnline.Admin
{
    public partial class UpdateSlideShow : AdminBasePage
    {
        private int SlideShowID
        {
            get
            {
                return string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ((AdminMaster)this.Master).SetPageTitle("Slide Show Module");
            if (!IsPostBack)
            {
                if (SlideShowID > 0)
                {
                    ShopOnline.BLL.SlideShow slide = ShopOnline.BLL.SlideShow.GetByID(SlideShowID);
                    if (slide == null)
                    {
                        ShowMessage("Your request not availble", true);
                        return;
                    }
                    txtTitle.Text = slide.s_Title;
                    txtDescription.Text = slide.s_Description;
                    txtURL.Text = slide.s_URL;
                    imgImage.ImageUrl = "~/" + Path.Combine(Utilities.GetKeySetting("SlideShowDirectory"), slide.s_FileName);
                    imgImage.Visible = true;
                    if (ckbShowAlways.Checked)
                    {
                        txtStartDate.Enabled = txtStartTime.Enabled = txtEndDate.Enabled = txtEndTime.Enabled = false;
                    }
                }
                
            }
        }

        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    string filename = string.Empty;
                    string[] arr = { "jpg", "png", "bmp", "gif" };

                    if(fulImage.HasFile && !arr.Contains<string>(Path.GetExtension(fulImage.FileName).ToLower().Replace(".", "")))
                    {
                        ShowMessage("Invalid image file", true); return;
                    }
                    else if (fulImage.HasFile)
                    {
                        filename = Guid.NewGuid().ToString() + Path.GetExtension(fulImage.FileName).ToLower();
                        //save image file
                        string path = Path.Combine(Server.MapPath("~"), Utilities.GetKeySetting("SlideShowDirectory"));
                        DirectoryInfo dInfo = new DirectoryInfo(path);
                        if (!dInfo.Exists)
                            dInfo.Create();
                        fulImage.SaveAs(Path.Combine(path, filename));
                    }
                    else if (!fulImage.HasFile && imgImage.Visible == true)
                    {
                        filename = Path.GetFileName(imgImage.ImageUrl);
                    }
                    else
                    {
                        ShowMessage("Select a image file", true); return;
                    }
                    
                    ShopOnline.BLL.SlideShow slide = new ShopOnline.BLL.SlideShow();
                    if (SlideShowID > 0)
                        slide = ShopOnline.BLL.SlideShow.GetByID(SlideShowID);
                    else
                        slide.AddNew();
                    slide.s_Title = txtTitle.Text.Trim();
                    slide.s_Description = txtDescription.Text.Trim();
                    slide.s_FileName = filename;
                    slide.s_URL = txtURL.Text.Trim();
                    slide.Active = true;
                    slide.Save();
                    if (SlideShowID > 0)
                        ShowMessage("Save successfully!", false);
                    else
                        Response.Redirect("~/Admin/SlideShow.aspx");

                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message, true);
                }
            }
        }

        private void ShowMessage(string mess, bool isError)
        {
            if(isError)
                lblMessage.Text = string.Format("<div class='message message-error'><span>{0}</span></div>", mess);
            else
                lblMessage.Text = string.Format("<div class='message message-success'><span>{0}</span></div>", mess);
        }
    }
}